﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace SkyReader
{
    public partial class SetBookmark : PhoneApplicationPage
    {
        private Book book;
        public SetBookmark()
        {
            InitializeComponent();

            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            string fileName = settings["VARIABLE_CURRENTFILENAME"].ToString();

            foreach (var setting in settings)
            {
                if (setting.Value is Book)
                {
                    book = setting.Value as Book;
                    if (book.FileName == fileName)
                    {
                        break;
                    }
                }
            }

            this.txtBookmark.Text = string.Format("bookmark {0}", book.MarkList.Count + 1);
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            Mark mark = new Mark();
            mark.Name = this.txtBookmark.Text.Trim();
            mark.PageIndex = book.CurrentPage;
            mark.DateCreated = DateTime.Now;
            book.MarkList.Add(mark);

            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings[book.Id] = this.book;
            settings.Save();

            MessageBox.Show("The bookmark has been added.", "Add bookmark successful", MessageBoxButton.OK);
            
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
    }
}